package com.apestech.api.workflow.extend;

import com.apestech.api.tables.ScriptInit;
import com.apestech.api.workflow.lib.element.WorkProcessEngine;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.DBUtil;

/**
 * 工作流与数据库操作有关的逻辑
 *
 * @author hup
 * @create 2018-07-13-10:05
 */
public class FlowDatabase {

    public class UserInfo{
        private String id;
        private String name;

        public void setId(String id) {
            this.id = id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }

    private SqlSession session;

    public FlowDatabase(SqlSession session){
        this.session=session;
    }

    public FlowDatabase(WorkProcessEngine engine) throws Exception {
        this.session= DBUtil.getSession(engine.getDataSource());
        this.session.setConnection(engine.getConnection());
    }

    public void iniDataBase() throws Exception {
        ScriptInit init=new ScriptInit(session);
        init.init();
    }

    public void startCommission(JSONObject input) throws Exception {
        String userId=input.getString("loginUserId");
        String userName=input.getString("loginUserName");
        String toUserId=input.getString("userId");
        String toUserName=input.getString("userName");
        if(userId==null){
            throw new Exception("当前登录人不允许为空！");
        }
        if(toUserId==null){
            throw new Exception("委托人不允许为空！");
        }
        if(userId.equals(toUserId)){
            throw new Exception("当前登录人不允许与委托人相同！");
        }
        String opinion=input.getString("opinion");
        String startDate=input.getString("startDate");
        String endDate=input.getString("endDate");
        startCommission(userId, userName,toUserId, toUserName,opinion, startDate, endDate);
    }

    public void startCommission(String userId, String userName,String toUserId, String toUserName,
                                String opinion, String startDate, String endDate) throws Exception {
        JSONObject r=new JSONObject();
        r.put("USER_ID", userId);
        if(!this.session.getForResultSet().queryIsEmpty("SELECT 1 FROM OA_COMMISSION WHERE 1=1 and CREATE_DATE=#{SYSDATE}",r)){
            throw new Exception("该人员正在委托中，如果需要修改委托，可先中止再执行委托操作即可！");
        }
        r.put("USER_NAME", userName);
        r.put("TO_USER_ID", toUserId);
        r.put("TO_USER_NAME", toUserName);
        r.put("CREATE_DATE", "SYSDATE");
        r.put("START_DATE", startDate);
        r.put("END_DATE", endDate);
        r.put("OPINION", opinion);
        this.session.getForUpdate().insertTable("OA_COMMISSION", r);
    }

    public void stopCommission(String userId) throws Exception {
        JSONObject r=new JSONObject();
        r.put("USER_ID", userId);
        if(this.session.getForUpdate().deleteTable("OA_COMMISSION", r)==0){
            throw new Exception("该人员不存在委托状态，不需要执行中止操作！");
        }
    }

    public UserInfo getCommission(String userId, String userName) throws Exception {
        JSONObject r=new JSONObject();
        r.put("USER_ID", userId);
        final UserInfo toUserId = new UserInfo();
//        String sql1="SELECT TO_USER_ID, TO_USER_NAME FROM OA_COMMISSION WHERE 1=1 AND ((START_DATE IS NULL AND END_DATE IS NULL) OR (START_DATE<=${SYSDATE} AND END_DATE>=${SYSDATE}) )";
//        this.session.getForResultSet().sql(sql1, "OA_COMMISSION",true, r,
//                new QueryCallBackHandler(){
//                    @Override
//                    public void getData(SqlResultSet rs) throws Exception {
//                        while(rs.next()){
//                            toUserId.setId(rs.getString("TO_USER_ID"));
//                            toUserId.setName(rs.getString("TO_USER_NAME"));
//                        }
//                    }
//                });
        if(toUserId.getId()==null) {
            toUserId.setId(userId);
            toUserId.setName(userName);
        }
        return toUserId;
    }

    public void writeNodeLog(String getProcessInstanceId, String businessKey,String userId, String userName,
                             String opinion, int agree,String fromUserId, String fromUserName, String counterUserId, String counterUserName,
                             String taskId, String taskName, int type) throws Exception {
        JSONObject r=new JSONObject();
        if(counterUserId==null){
            counterUserId=userId;
        }
        if(counterUserName==null){
            counterUserName=userName;
        }
        r.put("ID_",getProcessInstanceId);
        r.put("BUSINESS_KEY_", businessKey);
        r.put("TASK_ID", taskId);
        r.put("TASK_NAME", taskName);
        r.put("USER_ID", userId);
        r.put("USER_NAME", userName);
        r.put("CREATE_DATE", "SYSDATE");
        r.put("OPINION", opinion);
        r.put("AGREE", agree);
        r.put("TASK_TYPE", type);
        r.put("EXTEND_TABLE", null);
        r.put("FROM_USER_ID", fromUserId);
        r.put("FROM_USER_NAME", fromUserName);
        r.put("COUNTER_USER_ID", counterUserId);
        r.put("COUNTER_USER_NAME", counterUserName);
        this.session.getForUpdate().insertTable("OA_TASKLIST", r);
    }

}
